import Decimal from 'decimal.js';


export default {
  install: (app) => {
    // 扩展 Number 构造函数
    Number.prototype.toDecimal = function() {
      // 如果this是NaN，或者null，或者undefined，统一转换为数字0
      const value = isNaN(this) || this === null || this === undefined ? 0 : this;
      return new Decimal(value);
    };

    Number.prototype.add = function(other) {
      return this.toDecimal().plus(new Decimal(other)).toNumber();
    };

    Number.prototype.subtract = function(other) {
      return this.toDecimal().minus(new Decimal(other)).toNumber();
    };

    Number.prototype.multiply = function(other) {
      return this.toDecimal().times(new Decimal(other)).toNumber();
    };

    Number.prototype.divide = function(other) {
      return this.toDecimal().dividedBy(new Decimal(other)).toNumber();
    };
  }
};
